package com.bgds.sys.rbac.controller;

import com.bgds.sys.common.pagevo.PageVo;
import com.bgds.sys.common.pagevo.Result;

import com.bgds.sys.common.utils.PageUtil;
import com.bgds.sys.common.utils.ResultUtil;
import com.bgds.sys.rbac.domain.User;
import com.bgds.sys.rbac.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;



/**
 * @author lj
 */
@Slf4j
@RestController
@Api(description = "用户管理接口")
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;


    /**
     * 条件查询用户
     * @param key
     * @param page
     * @return
     */
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    @ApiOperation(value = "条件查询用户",notes = "条件查询用户",httpMethod = "GET",response = Result.class)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "key", dataType = "String",value = "关键字")
    })
    public Result<Page<User>> getUserByCondition(String key, @ModelAttribute PageVo page){
        Page<User> list = userService.listByCondition(key, PageUtil.initPage(page));
        return new ResultUtil<Page<User>>().setData(list);
    }

    @RequestMapping(value = "/updateUserRole", method = RequestMethod.PUT)
    @ApiOperation(value = "更新用户角色",notes = "更新用户角色",httpMethod = "PUT")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId", dataType = "Long", required = true, value = "用户ID"),
            @ApiImplicitParam(name = "roleIds", dataType = "Long[]", value = "角色id数组")
    })
    public Result<Object> getUserByCondition(@RequestParam Long userId,
                                             @RequestParam Long roleId){
        try {
            userService.updateUserRole(userId,roleId);
        }catch (Exception e){
            return new ResultUtil<Object>().setErrorMsg("更新失败");
        }

        return new ResultUtil<Object>().setSuccessMsg("更新成功");
    }
}
